class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<char,int>mp;
        int maxLen=0,k=0;
        for(int i=0;i<s.size();++i)
        {
            if(mp.find(s[i])==mp.end()||mp[s[i]]<k)
            {
                maxLen=max(maxLen,i-k+1);
            }
            else
            {
                k=mp[s[i]];
            }
            mp[s[i]]=i+1;//存下一个字符的下标
        }
        return maxLen;
    }
};